<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Trade;
use App\Models\BaseModel;
use App\Models\TradeCate;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Show;

class TradeController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Trade(), function (Grid $grid) {
            $grid->model()->latest('id');
            $grid->column('id')->sortable();
            $grid->column('cate_id')->display(function ($id) {
                return TradeCate::find($id)->cate_name ?? '';
            });
            $grid->column('trade_name');
            $grid->trade_img->image('', 50);
            $grid->column('views');
            $grid->column('sort')->sortable();
            $grid->column('status')->switch();
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
                $filter->equal('cate_id')->select(TradeCate::pluck('cate_name', 'id'));
                $filter->like('trade_name');
                $filter->equal('status')->select(BaseModel::statusMap());
                $filter->between('created_at')->datetime();
                $filter->between('updated_at')->datetime();
            });
            $grid->quickSearch(['id', 'trade_name']);
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Trade(), function (Show $show) {
            $show->field('id');
            $show->field('camp_id');
            $show->field('cate_id');
            $show->field('trade_name');
            $show->field('trade_img');
            $show->field('trade_details');
            $show->field('views');
            $show->field('sort');
            $show->field('status');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Trade(), function (Form $form) {
            $form->display('id');
            $form->select('cate_id')->options(TradeCate::all()->pluck('cate_name', 'id'))->rules('required');
            $form->text('trade_name')->rules('required|max:100');
            $form->image('trade_img')->autoUpload()->removable(false)->retainable();
            $form->array('trade_details', function ($table) {
                $table->text('title', '标题');
                $table->editor('con', '内容');
            })->saveAsJson();
            $form->number('sort')->type('number');
            $form->radio('is_consult')->options(BaseModel::isMap())->default(BaseModel::NO);
            $form->radio('status')->options(BaseModel::statusMap())->default(BaseModel::STATUS_DISABLE);
        });
    }
}
